﻿using System;
using System.Drawing;
using Data.Realm;

namespace Client
{
    public class MapField
    {
        public static readonly Color TYPE_DROPPED = Color.FromArgb(255, 190, 16);
        public static readonly Color TYPE_READY = Color.FromArgb(200, 200, 0);
        public static readonly Color TYPE_DEFAULT = Color.FromArgb(114, 46, 0);
        private Color color = TYPE_DEFAULT;

        private System.Windows.Forms.Panel panel = new System.Windows.Forms.Panel();

        private bool visited { get; set; }
        public string Plant { get; set; }
        public int Status { get; set; }
        public int DriveCost { get; set; }
        public int CoordianteY { get; set; }
        public int CoordianteX { get; set; }

        public System.Windows.Forms.Panel Panel
        {
            get { return this.panel; }
            set { this.panel = value; }
        }

        public Color Color
        {
            get { return this.color; }
            set { this.color = value; }
        }

        public MapField(int x, int y, int status, string plant, int driveCost)
        {
            this.CoordianteX = x;
            this.CoordianteY = y;
            this.Status = status;
            this.Plant = plant;
            this.DriveCost = this.GenerateCostDrive();
            this.Color = this.GenerateColorByStatus();
            this.Panel.Name = "panel" + this.CoordianteX + "_" + this.CoordianteY;
            lock (Tractor.MainForm.AgentPanel){
                Tractor.MainForm.AgentPanel.Controls.Add(this.Panel);
            }
        }

        public Color GenerateColorByStatus()
        {
            switch (this.Status)
            {
                case Data.Realm.Field.POLE_PUSTE:
                        this.Plant = "brak";
                        return TYPE_DEFAULT;

                case Data.Realm.Field.POLE_ZASIANE:
                        return TYPE_DROPPED;

                case Data.Realm.Field.POLE_GOTOWE:
                        return TYPE_READY;
                default:
                        return TYPE_DEFAULT;
            }   
        }

        public int GenerateCostDrive()
        {
            switch (this.Status)
            {
                case Data.Realm.Field.POLE_PUSTE:
                    return 30;

                case Data.Realm.Field.POLE_ZASIANE:
                    return 50;

                case Data.Realm.Field.POLE_GOTOWE:
                    return 20;
                default:
                    return 30;
            }
        }

        public Color GenerateColorByDriveCost()
        {
            var argb = Convert.ToInt32(this.DriveCost);
            return Color.FromArgb(argb);
        }

        public string DoubleToHex(double value, int maxDecimals)
        {
            string result = string.Empty;
            if (value < 0)
            {
                result += "-";
                value = -value;
            }
            if (value > ulong.MaxValue)
            {
                result += double.PositiveInfinity.ToString();
                return result;
            }
            ulong trunc = (ulong)value;
            result += trunc.ToString("X");
            value -= trunc;
            if (value == 0)
            {
                return result;
            }
            result += ".";
            byte hexdigit;
            while ((value != 0) && (maxDecimals != 0))
            {
                value *= 16;
                hexdigit = (byte)value;
                result += hexdigit.ToString("X");
                value -= hexdigit;
                maxDecimals--;
            }
            return result;
        }
    }
}
